package frpc.boot.centre;

import frpc.application.web.IRedisManagerController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.result.method.RequestMappingInfo;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;

import java.util.Map;

/**
 * 类描述: Redis管理接口注册器
 *
 * @author : 王雷
 * @date : 2022/6/22 0022 下午 7:02
 */
@Slf4j
//@Component
//BeanDefinitionRegistryPostProcessor
public class RedisControllerConfig {

    /**
     * 属性描述：Redis缓存处理交互前缀前缀
     *
     * @date : 2022/8/16 0016 下午 7:48
     */
    private static final String REDIS_CACHE_KEY_PREFIX = "/rm/manager/";

    protected RedisControllerConfig(RequestMappingHandlerMapping requestMappingHandlerMapping, IRedisManagerController redisManagerController) {
        log.info("开始加载Redis管理接口");
        //获取所有当前服务缓存描述
        registerInteractionObject(requestMappingHandlerMapping, redisManagerController
                , REDIS_CACHE_KEY_PREFIX + "giveRedisCacheList", "giveRedisCacheList", String.class);
        //获取其他未定义同服缓存
        registerInteractionObject(requestMappingHandlerMapping, redisManagerController
                , REDIS_CACHE_KEY_PREFIX + "giveRedisByKey", "giveRedisByKey", String.class);
        //获取其他未定义同服缓存
        registerInteractionObject(requestMappingHandlerMapping, redisManagerController
                , REDIS_CACHE_KEY_PREFIX + "giveRedisAllToList", "giveRedisAllToList");
        //移除缓存
        registerInteractionObject(requestMappingHandlerMapping, redisManagerController
                , REDIS_CACHE_KEY_PREFIX + "delRedisCacheByKey", "delRedisCacheByKey", String.class);
        //获取缓存详细信息
        registerInteractionObject(requestMappingHandlerMapping, redisManagerController
                , REDIS_CACHE_KEY_PREFIX + "giveRedisCacheByKey", "giveRedisCacheByKey", String.class);
        //清空Redis缓存描述
        registerInteractionObject(requestMappingHandlerMapping, redisManagerController
                , REDIS_CACHE_KEY_PREFIX + "clearRedisCacheDescription", "clearRedisCacheDescription");
        //添加缓存描述
        registerInteractionObject(requestMappingHandlerMapping, redisManagerController
                , REDIS_CACHE_KEY_PREFIX + "saveOrUpdateCacheDescribe", "saveOrUpdateCacheDescribe"
                , String.class, String.class);
        //删除缓存描述
        registerInteractionObject(requestMappingHandlerMapping, redisManagerController
                , REDIS_CACHE_KEY_PREFIX + "removeCacheDescribe", "removeCacheDescribe", String.class);
    }

    private void registerInteractionObject(RequestMappingHandlerMapping requestMappingHandlerMapping
            , IRedisManagerController redisManagerController, String path, String methodName, Class<?>... parameterTypes) {

        RequestMappingInfo mappingInfo = RequestMappingInfo.paths(path).build();

        try {
            //注册Mapping信息逻辑
            requestMappingHandlerMapping.registerMapping(mappingInfo, redisManagerController, redisManagerController.getClass()
                    .getDeclaredMethod(methodName, parameterTypes));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        final Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        log.debug("map：{}", handlerMethods);
        handlerMethods.forEach((requestMappingInfo, handlerMethod) -> {
            log.debug("requestMappingInfo：{}", requestMappingInfo);
            log.debug("handlerMethod：{}", handlerMethod);
        });
    }

    /*private void registerInteractionObject(){
        RequestMappingHandlerMapping requestMappingHandlerMapping = this.applicationContext.getBean("requestMappingHandlerMapping"
                , RequestMappingHandlerMapping.class);

        RequestMappingInfo mappingInfo = RequestMappingInfo.paths("/rm/manager").build();
        //通过Spring IOC容器获取对应的bean实例
        Object bean = applicationContext.getBean("redisManagerControllerBean");

        try {
            //注册Mapping信息逻辑
            requestMappingHandlerMapping.registerMapping(mappingInfo, bean, bean.getClass()
                    .getDeclaredMethod("giveRedisCacheList"));
            requestMappingHandlerMapping.registerMapping(mappingInfo, bean, bean.getClass()
                    .getDeclaredMethod("delRedisCacheByKey", String.class));
            requestMappingHandlerMapping.registerMapping(mappingInfo, bean, bean.getClass()
                    .getDeclaredMethod("giveRedisCacheByKey", String.class));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        final Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        log.error("map：{}", handlerMethods);
        handlerMethods.forEach((requestMappingInfo, handlerMethod) -> {
            log.error("requestMappingInfo：{}", requestMappingInfo);
            log.error("handlerMethod：{}", handlerMethod);
        });
    }*/
}
